/***
 *
 * 用于更改模型内的材质，主要是动态更改纹理和纹理的显隐 Material.js
 *
 * Daniel
 * 2018.03.13
 *
 * ***/
define(function(){
    
    var Texture = function(){
        
        this.path = "";
        this.scene = {};
        this.planeParameter = {
            width: 2400,
            height: 2100,
            map: '',
            position: new THREE.Vector3(0,0,0),
            rotation: new THREE.Vector3(0,0.5*Math.PI,0),
            wrapS:2000,
            wrapT:2000
        };
        this.textureMangers = {};
        this.planeObject = {};
        
        
    };
    
    Texture.prototype.init = function (scene,path,plan) {
        
            this.path = path;
            this.planeParameter = plan;
            this.scene = scene;
    };
    
    //显示平面贴图
    Texture.prototype.showPlaneImage = function (imagePath){
        
        var textureArr = {
            materialArray:[],
            material:{}
        };
        var scope = this;
        var path = imagePath;
        function createPlane(prams){
            var width = prams.width;
            var height = prams.height;
            var map = prams.map;
            var position = prams.position;
            var rotation = prams.rotation;
            var geometry = new THREE.PlaneGeometry( width, height);
            var material = new THREE.MeshBasicMaterial( {map: map,side:2} );
        
            //通道设置
            //material.opacity = 0.9;
            
            material.alphaMap = map;
            material.transparent = true;
        
            var plane = new THREE.Mesh( geometry, material );
            //plane.position.set(position.x,position.y,position.z);
            var group = new THREE.Group();
        
            group.add(plane);
        
            plane.rotation.set(-0.5*Math.PI,0,0);
            group.rotation.set(rotation.x,rotation.y,rotation.z);
            group.position.set(position.x,position.y,position.z);
            return group;
        }
        
        this.textrueChange(textureArr,path,function(map){
            var wrapT = scope.planeParameter.wrapT;
            var wrapS = scope.planeParameter.wrapS;
            map.wrapT = wrapT;
            map.wrapS = wrapS;
            scope.planeParameter.map = map;
            var plane = createPlane(scope.planeParameter);
            plane.name = "1231313";
            plane.visible = true;
            if( scope.planeObject) scope.scene.remove(scope.planeObject);
                scope.planeObject = plane;
                scope.scene.add(plane);
        });
        
    };
    
    //隐藏平面图层
    Texture.prototype.hidePlaneImage = function(){
        var scope = this;
        return function(boolean){
            var plane = scope.planeObject;
                plane.parent.visible = boolean;
        }
        
    };
    
    //移除平面图层
    Texture.prototype.removePlane = function(){
        this.scene.remove(this.planeObject);
    };
    
    // //显示局部区域的图层
    // Texture.prototype.viewPicture = function (boolean){
    //     if(boolean){
    //         container.addEventListener("click",showViewPicture,false);
    //     }else {
    //         container.removeEventListener("click",showViewPicture,false);
    //     }
    //
    // };
    //
    // function showViewPicture(event){
    //
    //     var object = data.temporary;
    //
    //     if(object){
    //
    //         var id = object.id;
    //         data.viewPicture.style.display = "block";
    //         data.viewPicture.src = data.viewPicturePath + Math.floor(Math.random()*10)+ ".jpg";         //更换相应的图层
    //
    //     }else{
    //         data.viewPicture.style.display = "none";
    //     }
    // }
    
    //更换平面图层的渲染方式
    Texture.prototype.changeMaterial = function (){
        var scope = this;
        return function (taggle) {
            var objects = scope.planeObject;
            if(taggle !== undefined){
                objects.parent.visible = true;
        
                if(taggle){
            
                    objects.material.opacity = 0.7;
                    objects.material.alphaMap = null;
                    objects.material.transparent = true;
                    objects.material.needsUpdate = true;
            
                }else{
            
                    objects.material.opacity = 1;
            
                    var map = objects.material.map;
            
                    objects.material.alphaMap = map;
                    objects.material.transparent = true;
                    objects.material.needsUpdate = true;
            
                }
            }else{
        
                objects.parent.visible = false;
        
            }
        };
       
    };
    
    //材质管理调用
    Texture.prototype.materialControl = function (object,name){
        
        var texture = this.textureMangers.material[name];
            this.materialManger(object, texture);
            
    };
    
    //材质管理
    Texture.prototype.materialManger = function (object3d,texture){
        if (object3d !== undefined){
            var mesh = object3d;
            mesh.material.map = texture;
            mesh.material.needsUpdate = true;
        } else {
            console.error("传入的参数不是object3D对象！");
        }
    };
    
    //纹理切换列表
    Texture.prototype.textrueChange = function (texture,path,fun){
        if (texture)
        {
            var length = path.length;
            var loader = new THREE.TextureLoader();
            for (var i = 0; i < length; i++)
            {
                var pathArray = path[i];
                loader.load(pathArray, function (text)
                {
                    text.wrapS = text.wrapT = 20;
                    text.name = pathArray;
                    texture.material["" + text.name + ""] = text;
                    texture.materialArray.push(text);
                    if(fun!==undefined)fun(text);
                });
            }
        }
    };
    Texture.prototype.resetTextrue = function (object3d){
        if (object3d)
        {
            var mesh = object3d;
            mesh.material.map = null;
            mesh.material.needsUpdate = true;
        }
    };
    
   
    
    return Texture;
    
});